package com.luocj.brvah_srl.activity;

import android.print.PrinterId;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.luocj.brvah_srl.R;
import com.luocj.brvah_srl.adapter.MultiAdapter;
import com.luocj.brvah_srl.entity.MultiModel;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;

import java.util.ArrayList;
import java.util.Random;

public class MultiItemActivity extends AppCompatActivity {

    private SmartRefreshLayout smartrefreshlayout;
    private RecyclerView recyclerview;
    private ArrayList<MultiModel> multiModels;
    private MultiAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_multi_item);
//        initData();

        initView();
    }

    private ArrayList<MultiModel> initData() {
        multiModels = new ArrayList<>();
        for (int i = 0; i < 20; i++) {
//            if (i % 3 == 0) {
//                multiModels.add(new MultiModel(1, 2));
//            } else {
//                multiModels.add(new MultiModel(2, 1));
//            }
            int num = (int) (Math.random() * 4);
            if (num % 3 == 0) {
                multiModels.add(new MultiModel(1, 2));
            } else {
                multiModels.add(new MultiModel(2, 1));
            }
        }
        return multiModels;
    }

    private void initView() {
        smartrefreshlayout = findViewById(R.id.smartrefreshlayout_multi);
        recyclerview = findViewById(R.id.recyclerview_multi);
//        LinearLayoutManager layoutManager = new LinearLayoutManager(MultiItemActivity.this, LinearLayoutManager.VERTICAL, false);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 2);
        recyclerview.setLayoutManager(gridLayoutManager);


        adapter = new MultiAdapter(MultiItemActivity.this, null);
        adapter.setSpanSizeLookup(new BaseQuickAdapter.SpanSizeLookup() {
            @Override
            public int getSpanSize(GridLayoutManager gridLayoutManager, int position) {
                return adapter.getData().get(position).getSpanSize();
            }
        });
        recyclerview.setAdapter(adapter);
        smartrefreshlayout.setEnableAutoLoadmore(true);
        smartrefreshlayout.autoRefresh();

        smartrefreshlayout.setOnRefreshLoadmoreListener(new OnRefreshLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                refresh(refreshlayout, false);
            }

            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                refresh(refreshlayout, true);
            }
        });
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Toast.makeText(MultiItemActivity.this,"position :" + position,Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void refresh(RefreshLayout refreshlayout, final boolean isrefresh) {
        refreshlayout.getLayout().postDelayed(new Runnable() {
            @Override
            public void run() {
                if (isrefresh) {
                    adapter.setNewData(initData());
                    smartrefreshlayout.finishRefresh();
                    smartrefreshlayout.setLoadmoreFinished(false);
                } else {
                    adapter.addData(initData());
                    smartrefreshlayout.finishLoadmore();
                }
            }
        }, 2000);
    }
}
